package particle.calibration;

import java.awt.Point;
import java.util.ArrayList;

import processing.core.PVector;

public class Pyramide_View {
	
	Calibration_Tuple[] points;
	int min_y = Integer.MAX_VALUE;
	int max_y = 0;
	int min_x = Integer.MAX_VALUE;
	int max_x = 0;
	PVector center;
	
	public Pyramide_View(ArrayList<Calibration_Tuple> calib_markers, int index_start) {
		
		points = new Calibration_Tuple[5];
		for(int i = 0; i < 5; i++){
			points[i] = new Calibration_Tuple(calib_markers.get(i+index_start).x, calib_markers.get(i+index_start).y);
		}
		
		center = new PVector(points[4].x, points[4].y);
		
		//calculate average values
		System.out.println("----------------");
		int mid_y0 = (points[0].y +points[1].y) /2; 
		points[0].y = mid_y0;
		points[1].y = mid_y0;
		int mid_y1 = (points[2].y +points[3].y) /2; 
		points[2].y = mid_y1;
		points[3].y = mid_y1;
		
		int mid_x0 = (points[0].x +points[2].x) /2; 
		points[0].x = mid_x0;
		points[2].x = mid_x0;
		int mid_x1 = (points[1].x +points[3].x) /2; 
		points[1].x = mid_x1;
		points[3].x = mid_x1;
		
		
		//calc min max values
		for(int i = 0; i < 5; i++){
			if(points[i].x  < min_x){
				min_x = points[i].x;
			}
			if(points[i].x  > max_x){
				max_x = points[i].x;
			}
			if(points[i].y < min_y){
				System.out.println("min:"+points[i].y);
				min_y = points[i].y;
			}
			if(points[i].y > max_y){
				max_y = points[i].y;
			}
		}

	}
	
	public boolean isInPyramide(PVector o){
		if(o.x >= min_x && o.x <= max_x && o.y >= min_y && o.y <= max_y) return true;
		else return false;
	}
	
	public float getValue(PVector o){
		
		//use vectors to calculate angle to center
		PVector sub = PVector.sub(center, o);
		double angle = (((Math.atan2(sub.y, sub.x)*(180/Math.PI)))+360)%360;
		return (float) angle/360;
	}

}
